package com.admin.controller;


import com.admin.entity.Class;
import com.admin.entity.Dormitory;
import com.admin.service.DormitoryService;
import com.admin.utils.Result;
import com.admin.vo.query.ClassQueryVo;
import com.admin.vo.query.DormitoryQueryVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.annotation.Resource;

/**
 * <p>
 * 宿舍表 前端控制器
 * </p>
 *
 * @author szy
 * @since 2023-03-02
 */
@RestController
@RequestMapping("/api/dormitory")
public class DormitoryController {

    @Resource
    private DormitoryService dormitoryService;

    /*分页查询*/
    @GetMapping("/list")
    public Result list(DormitoryQueryVo dormitoryQueryVo){
        IPage<Dormitory> page = new Page<Dormitory>(dormitoryQueryVo.getPageNo(), dormitoryQueryVo.getPageSize());
        dormitoryService.getDormitoryByPage(page,dormitoryQueryVo);
        return Result.ok(page);
    }

    @PostMapping("/add")
    public Result add(@RequestBody Dormitory dormitory){
        if (dormitoryService.save(dormitory)){
            return Result.ok().message("添加成功");
        }else{
            return Result.error().message("添加失败");
        }
    }

    @PutMapping("/update")
    public Result update(@RequestBody Dormitory dormitory){
        if (dormitoryService.updateById(dormitory)){
            return Result.ok().message("修改成功");
        }else{
            return Result.error().message("修改失败");
        }
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        if (dormitoryService.removeById(id)){
            return Result.ok().message("删除成功");
        }else{
            return Result.error().message("删除失败");
        }
    }
}

